package com.cdigtcwww01.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cdigtcwww01.dao.UserDao;
import com.cdigtcwww01.model.User;

@WebServlet(value="/UserController", name="UserController")
public class UserController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static String INSERT_OR_EDIT = "/user.jsp";
    private static String LIST_USER = "/listUser.jsp";
    private UserDao dao;

    public UserController() {
        super();
        dao = new UserDao();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String forward="";
        String action = request.getParameter("action");

        if (action.equalsIgnoreCase("delete")){
            String userId = request.getParameter("userId");
            dao.deleteUser(userId);
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());
        } else if (action.equalsIgnoreCase("edit")){
            forward = INSERT_OR_EDIT;
            String userId = request.getParameter("userId");
            User user = dao.getUserById(userId);
            request.setAttribute("user", user);
        } else if (action.equalsIgnoreCase("listUser")){
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());
        } else {
            forward = INSERT_OR_EDIT;
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        String action = request.getParameter("action");
        user.setEmpresa(request.getParameter("empresa"));
        user.setCargo(request.getParameter("cargo"));
        user.setFuncao(request.getParameter("funcao"));
        user.setLotacao(request.getParameter("lotacao"));
        user.setNome(request.getParameter("nome"));
        user.setRamal(request.getParameter("ramal"));
        user.setUnidade(request.getParameter("unidade"));
        user.setPrefixo(request.getParameter("prefixo"));
        String userid = request.getParameter("matricula");
        
//        if (action.equalsIgnoreCase("UserController")){
//        	user.setMatricula(userid);
//        	dao.addUser(user);
//        }else if(action.equalsIgnoreCase("edit")) {
//        	user.setMatricula(userid);
//        	dao.updateUser(user);
//        }
//        if(userid == null || userid.isEmpty())
//        {
//            dao.addUser(user);
//        }
//        else
//        {
//            user.setMatricula((userid));
//            dao.addUser(user);
//        }
        
        User usuarioConsulta = dao.getUserById(userid);
        if(usuarioConsulta.getMatricula() == null)
          {
        	user.setMatricula(userid);
              dao.addUser(user);
          }
          else
          {
              user.setMatricula((userid));
              dao.updateUser(user);
          }
        
        RequestDispatcher view = request.getRequestDispatcher(LIST_USER);
        request.setAttribute("users", dao.getAllUsers());
        view.forward(request, response);
    }
}